<?php

namespace app\award;

use app\models\order\Order;
use app\models\order\OrderItem;
use app\models\product\Product;
use app\models\product\ProductAreaList;
use app\models\SetProductCommission;
use app\models\SetUserUp;
use app\models\user\User;
use app\models\user\UserAccount;
use app\models\user\UserCommission;
use app\models\user\UserLevel;
use app\models\UserQuota;
use app\services\Chain;

class Up
{
    protected $order;
    protected $set;
    protected $user;

    // 商品ID
    protected $product_id;

    public function __construct(Order $order)
    {
        $this->order = $order;
        $this->set = SetUserUp::getLastOne();
        $this->user = User::findOne(['id' => $this->order->user_id]);

        $first_order_item = OrderItem::find()
            ->where(['order_id' => $this->order->id])
            ->orderBy(['id' => SORT_ASC])
            ->limit(1)
            ->one();
        $product_id = $first_order_item->product_id;
        $this->product_id = $product_id;

        // 报单区
        if (ProductAreaList::checkInArea($product_id, Product::$areaMap['REPORT'])) {
            $this->upPrice();
            // $this->upNumber();
            // $this->v1();
        }

        // 补差价区
        if (ProductAreaList::checkInArea($product_id, Product::$areaMap['BU'])) {
            $this->bu();
            // $this->upNumber();
            // $this->v1();


        }
    }

    public function bu()
    {
        $set = SetProductCommission::findOne(['product_id' => $this->product_id]);
        $level = $set->b_level;

        // var_dump($level);exit;

        if ($this->user->level < $level) {
            $this->user->level = $level;
            $this->user->save();

            // 配额
            // 后面说, 补差价不给配额
            // $quota = 0;
            // if ($level == UserLevel::$keys['vip2']) {
            //     $quota = $this->set->p_vip2;
            // } else if ($level == UserLevel::$keys['vip3']) {
            //     $quota = $this->set->p_vip3;
            // } else if ($level == UserLevel::$keys['vip4']) {
            //     $quota = $this->set->p_vip4;
            // } else if ($level == UserLevel::$keys['vip5']) {
            //     $quota = $this->set->p_vip5;
            // }
            //
            // $this->quota($quota);
        }
    }

    // 配额
    public function quota($n, $level)
    {
        $account = UserAccount::findOne(['user_id' => $this->user->id]);
        if ($account) {

            if ($level == 4) {
                $account->quota4 += $n;
            }
            if ($level == 3) {
                $account->quota3 += $n;
            }
            if ($level == 2) {
                $account->quota2 += $n;
            }
            if ($level == 1) {
                $account->quota += $n;
            }

            $account->save();

            // 日志
            $log = new UserQuota();
            $log->user_id = $this->user->id;
            $log->order_id = $this->order->id;
            $log->quota = $n;
            $log->level = $level;
            $log->save();
        }
    }

    // 是否推了两单
    // public function v1()
    // {
    //     $r = Chain::getReferrer($this->user->id, true);
    //     if ($r && $r->level >= UserLevel::$keys['vip1'] && $r->v1 == 0) {
    //         $count = (int) User::find()
    //             ->where(['referrer_id' => $r->id])
    //             ->andWhere(['>=', 'level', UserLevel::$keys['vip1']])
    //             ->count();
    //         if ($count >= 2) {
    //             $r->v1 = 1;
    //             $r->save();
    //         }
    //     }
    // }

    public function upPrice()
    {
        $amount = $this->order->total_pay - $this->order->total_freight;

        // var_dump($amount);exit;

        // var_dump($amount == $this->set->vip5, $amount , $this->set->vip5);exit;

        // 私董
        if ($amount == $this->set->vip5) {
            if ($this->user->level < UserLevel::$keys['vip5']) {
                $this->user->level = UserLevel::$keys['vip5'];
                $this->user->save();

                $this->quota($this->set->p_vip5, 4);

            }
        }
        // 董事
        else if ($amount == $this->set->vip4) {
            if ($this->user->level < UserLevel::$keys['vip4']) {
                $this->user->level = UserLevel::$keys['vip4'];
                $this->user->save();
                $this->quota($this->set->p_vip4, 3);

            }
        }
        // 资源股东
        else if ($amount == $this->set->vip3) {
            if ($this->user->level < UserLevel::$keys['vip3']) {
                $this->user->level = UserLevel::$keys['vip3'];
                $this->user->save();
                $this->quota($this->set->p_vip3, 2);

            }
        }
        // 联创
        else if ($amount == $this->set->vip2) {
            if ($this->user->level < UserLevel::$keys['vip2']) {
                $this->user->level = UserLevel::$keys['vip2'];
                $this->user->save();
                $this->quota($this->set->p_vip2, 1);

            }
        }
        // 大使
        else if ($amount == $this->set->vip1) {
            if ($this->user->level < UserLevel::$keys['vip1']) {
                $this->user->level = UserLevel::$keys['vip1'];
                $this->user->save();

            }
        }


    }

    // public function upNumber()
    // {
    //
    //     // 找上级
    //     $user = Chain::getReferrer($this->user->id, true);
    //     if (!$user) {
    //         return;
    //     }
    //
    //     if ($user->level < UserLevel::$keys['vip2']) {
    //         return;
    //     }
    //
    //     $children = Chain::getChildrenByLevel($user->id, [
    //         UserLevel::$keys['vip2'],
    //         UserLevel::$keys['vip3'],
    //         UserLevel::$keys['vip4'],
    //         UserLevel::$keys['vip5'],
    //     ]);
    //     $count = count($children);
    //
    //     if ($user->level == UserLevel::$keys['vip4']) {
    //         if ($count >= $this->set->vip5_n) {
    //             $user->level = UserLevel::$keys['vip5'];
    //             $user->save();
    //         }
    //     }
    //
    //     if ($user->level == UserLevel::$keys['vip3']) {
    //         if ($count >= $this->set->vip4_n) {
    //             $user->level = UserLevel::$keys['vip4'];
    //             $user->save();
    //         }
    //     }
    //
    //     if ($user->level == UserLevel::$keys['vip2']) {
    //         if ($count >= $this->set->vip3_n) {
    //             $user->level = UserLevel::$keys['vip3'];
    //             $user->save();
    //         }
    //     }
    // }

}